<template>
  <div class="search_container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>借阅信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="always">
      <!-- 搜索内容和导出区域 -->
      <el-row class="nav">
        <el-col :span="1.5" class="tip">条件搜索：</el-col>
        <el-col :span="4">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select"
            @keyup.enter.native="searchBookBorrowByPage">
            <el-button slot="append" icon="el-icon-search" @click="searchBookBorrowByPage"></el-button>
          </el-input></el-col>
        <!-- 添加借阅证 -->
        <el-col :span="4" class="add">
          <el-button type="primary" @click="addStatement">添加</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="tableData" border style="width: 100%" stripe id="pdfDom"
        :default-sort="{ prop: 'cardNumber', order: 'ascending' }" v-loading="loading" element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-table-column prop="cardNumber" label="借阅证编号" sortable>
        </el-table-column>
        <el-table-column prop="cardRules" label="借阅规则" sortable>
        </el-table-column>
        <el-table-column prop="cardStatus" label="状态" sortable>
        </el-table-column>
        <el-table-column prop="borrowDate" label="操作" sortable>
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                @click="showEditDialog(scope.row.cardNumber)"></el-button></el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页查询区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pageNum" :page-sizes="[1, 2, 3, 4, 5]" :page-size="this.queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="this.total">
      </el-pagination>
    </el-card>
    <!-- 添加 -->
    <el-dialog title="添加借书证" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <el-form :model="addForm" ref="addFormRef" :rules="addFormRules" label-width="120px">
        <el-form-item label="规则" prop="ruleNumber">
          <el-select v-model="addForm.ruleNumber" placeholder="请选择">
            <el-option v-for="item in bookRuleIdLists" :key="item.ruleId" :label="item.bookRuleId"
              :value="item.bookRuleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="userStatus">
          <el-radio-group v-model="addForm.userStatus">
            <el-radio label="可用">可用</el-radio>
            <el-radio label="禁用">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStatement">添加借阅证</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          cardNumber: '',
          cardRules: '',
          cardStatus: 1,
        },
      ],
      addForm: {
        ruleNumber: "",
        userStatus: "",
      },
      queryInfo: {
        pageNum: 1,
        pageSize: 5,
        condition: "",
        query: "",
      },
      total: 0,
      title: "图书借阅表格",
      json_fields: {
        借阅编号: "cardNumber",
        图书编号: "bookNumber",
        借阅日期: "borrowDate",
        截止日期: "closeDate",
        归还日期: "returnDate",
      },
      loading: false, // 是否显示加载中
      addDialogVisible: false,// 添加借阅证对话框的visible属性
    };
  },
  methods: {
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;

      this.searchBookBorrowByPage();
    },
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val;
    },

    // 根据当前登录用户查询借阅证
    async getStatementList() {
      this.loading = true;  // 显示加载中
      const { data: res } = await this.$http.get("user/get_statement_by_user/");
      if (res.status !== 200) {
        this.loading = false;
        return this.$message.error(res.msg);
      }
      this.tableData = res.data;
      this.total = res.data.length;
      this.loading = false;
      this.$message.success('查询成功');
    },
    // 添加借书证
    addStatement() {
      this.addDialogVisible = true;
    }
  },
  created() {
    // 获取借阅证列表
    this.getStatementList();
  },
};
</script>
<style scoped>
.nav {
  width: 100%;
}

.tip {
  height: 40px;
  line-height: 40px;
}

.add {
  float: right;
}
</style>